<?php
    class Archives extends Admin_Controller {

        public function index()
        {

            $this->load->model('admin/Sort_model');
            $treelist=$this->Sort_model->get_sort_all();
            $data['treelist']=$treelist;


            $dopost = $this->input->get('dopost',true); 
            // 分类ID
            $typeid = $this->input->get('typeid',true); 
            //关键字
            $keyword = $this->input->get('keyword',true); 
            //属性
            $flag = $this->input->get('flag',true); 
            //是否取分类及子分类(0是1否)
            $typeflag = $this->input->get('typeflag',true); 


            $whereSql =" WHERE arc.typeid=sot.id ";
            if($keyword != '')
            {
                $whereSql .= " AND ( CONCAT(arc.title,arc.writer) LIKE '%$keyword%') ";
            }
            if($flag != '')
            {
                $whereSql .= " AND FIND_IN_SET('$flag', arc.flag) ";
            }
            if($typeid != 0&&$typeid !='')
            {
                if($typeflag==0)
                {
                    $typeid=$typeid.','.$this->_sort_id_child_list($typeid);
                    $typeid=rtrim($typeid,',');
                    $whereSql .= ' AND arc.typeid IN ('.$typeid.')';
                }
                else
                {
                    $whereSql .= ' AND arc.typeid = ('.$typeid.')';
                }
            }



            $query = "SELECT sot.name as cname,arc.* FROM `crazy_archives` arc,`crazy_sort` sot $whereSql ORDER BY arc.uptime DESC";
            $this->load->model('admin/Archives_model');
            $list=$this->Archives_model->get_search($query);


            //从数据库中获取列表总数
            $num= $list->num_rows();

            $search_str="dopost=$dopost&typeid=$typeid&keyword=$keyword&flag=$flag";
            $params = array('total' => $num, 'listRows' => '10','pa'=>$search_str);

            $this->load->library('Page',$params);

            $query= $query."  limit {$this->page->limit} ";
            $reslut=$this->Archives_model->get_search($query);

            $data['list']=$reslut;

            $data['page']= $this->page->fpage();
            
             //获取自定义属性列表   
            $this->load->model('admin/Archives_model');
            $data['arcatt_all_list']= $this->Archives_model->get_arcatt_all();
            
            $this->load->view('admin/archives_index',$data);
        }

        //新闻添加
        public function add()
        {     
            //获取分类          
            $this->load->model('admin/Sort_model');
            $treelist=$this->Sort_model->get_sort_all();
            $data['treelist']=$treelist;

            //获取自定义属性列表   
            $this->load->model('admin/Archives_model');
            $data['arcatt_all_list']= $this->Archives_model->get_arcatt_all();

            $this->load->view('admin/archives_add',$data);
        }

        public function save()
        {
            $this->load->helper('common_function');
            $dopost = $this->input->post('dopost',true); 
            $this->load->model('admin/Archives_model');
            //加载CI日期辅助函数
            $this->load->helper('date');
            if($dopost=='add')
            {
                //添加数据


                //分类ID
                $typeid = $this->input->post('typeid',true); 
                //自定义属性
                $flags =$this->input->post('flags',true);
                $flag = !empty($flags) ? join(',',$flags) : '';

                //标题
                $title = $this->input->post('title',true); 
                //作者
                $writer = $this->input->post('writer',true); 
                //来源
                $source = $this->input->post('source',true); 
                //内容摘要
                $description = $this->input->post('description',true); 
                //内容
                $arcids = $this->input->post('content',true); 
                //图片地址
                $litpic = $this->input->post('url1',true);
                //发布时间
                $sendtime = human_to_unix($this->input->post('sendtime',true));
                //更新时间
                $uptime = now();
                //创建时间
                $createtime = now(); 
                $data = array(
                    'typeid' => $typeid ,
                    'flag' => $flag,
                    'title' => $title,
                    'writer' => $writer,
                    'source' => $source,
                    'description' => $description,
                    'arcids' => $arcids,
                    'litpic' => $litpic,
                    'sendtime' => $sendtime,
                    'uptime' => $uptime,
					  'createid' => $this->_admin->id,
					   'createname' => $this->_admin->userid,
                    'createtime' => $createtime,
                );

                $this->Archives_model->archives_save_add($data);
                ShowMsg('添加成功!', site_url("admin/archives"),0,1000);
                exit; 
            }
            if($dopost=='modify')
            {
                //编辑数据  

                $id= $this->input->post('id',true);
                //分类ID
                $typeid = $this->input->post('typeid',true); 
                //自定义属性
                $flags =$this->input->post('flags',true);

                $flag = empty($flags) ? '':join(',',$flags);
                //标题
                $title = $this->input->post('title',true); 
                //作者
                $writer = $this->input->post('writer',true); 
                //来源
                $source = $this->input->post('source',true); 
                //内容摘要
                $description = $this->input->post('description',true); 
                //内容
                $arcids = $this->input->post('content',true); 
                //图片地址
                $litpic = $this->input->post('url1',true);
                //发布时间
                $sendtime = human_to_unix($this->input->post('sendtime',true));
                //更新时间
                $uptime = now();
                $data = array(
                    'typeid' => $typeid ,
                    'flag' => $flag,
                    'title' => $title,
                    'writer' => $writer,
                    'source' => $source,
                    'description' => $description,
                    'arcids' => $arcids,
                    'litpic' => $litpic,
                    'sendtime' => $sendtime,
					  'createid' => $this->_admin->id,
					   'createname' => $this->_admin->userid,
                    'uptime' => $uptime,
                );
                $this->Archives_model->get_archives_modify($id,$data);
				
				$backurl=$this->input->post('backurl',true);
				if($backurl=="0")
				{
					$backurl=site_url("admin/archives");
				}
				
                ShowMsg('编辑成功!',$backurl ,0,1000);
                exit; 
                // $this->output->enable_profiler(TRUE);
            }
        }


        //编辑文章
        public function modify()
        {
            $this->load->helper('common_function');
            $id = $this->input->get('id',true);  
            $this->load->model('admin/Archives_model');
            $model=$this->Archives_model->get_archives_by_id_one($id);
            if($model)
            {
                //获取分类
                $this->load->model('admin/Sort_model');
                $treelist=$this->Sort_model->get_sort_all();
                $data['treelist']=$treelist;

                //获取自定义属性列表   
                $data['arcatt_all_list']= $this->Archives_model->get_arcatt_all();

                $data['model']=$model;
                $this->load->view('admin/archives_modify',$data);

            }
            else
            {
                ShowMsg('文章不存在!',-1,0,1000);
                exit; 
            }
        }


        public function del()
        {
            $this->load->helper('common_function');
            $qstr = $this->input->get('qstr',true);
            $aid = $this->input->get('aid',true);    
            $dopost = $this->input->get('dopost',true);    
            $arcids = preg_replace("#[^0-9,]#", '', preg_replace("#`#", ',', $qstr));
            $arr_arcids=explode(',',$arcids);
            $this->load->model('admin/Archives_model');

            foreach($arr_arcids as $var)
            {

                $this->Archives_model->get_archives_del($var);
            } 
            ShowMsg('删除成功!', site_url("admin/archives"),0,1000);
            exit; 
        }

        //返回与某个目相关的下级目录的类目ID列表
        public function _sort_id_child_list($id)
        {
            $total_id='';
            $this->load->model('admin/Sort_model');
            $model=$this->Sort_model->get_sort_by_pid($id);
            if ($model->num_rows() > 0)
            {
                foreach($model->result() as $row)
                {
                    $total_id=$total_id.$row->id.','; 
                    $total_id=$total_id.$this->_sort_id_child_list($row->id);
                }
            }

            return $total_id;
        }
		
		
		
		
		
		
		
		
		
		 public function member()
        {

         


            $dopost = $this->input->get('dopost',true); 
            // 分类ID
            $typeid = $this->input->get('typeid',true); 
            //关键字
            $keyword = $this->input->get('keyword',true); 
            //属性
            $flag = $this->input->get('flag',true); 
            //是否取分类及子分类(0是1否)
            $typeflag = $this->input->get('typeflag',true); 
			
			
			
			$typeid_s=$typeid.','.$this->_sort_id_child_list($typeid);
            $typeid_s=rtrim($typeid_s,',');
			
			$this->load->model('admin/Sort_model');
            $treelist=$this->Sort_model->get_sort_by_id($typeid_s);
            $data['treelist']=$treelist;
			
			


            $whereSql =" WHERE arc.typeid=sot.id ";
            if($keyword != '')
            {
                $whereSql .= " AND ( CONCAT(arc.title,arc.writer) LIKE '%$keyword%') ";
            }
            if($flag != '')
            {
                $whereSql .= " AND FIND_IN_SET('$flag', arc.flag) ";
            }
            if($typeid != 0&&$typeid !='')
            {
                if($typeflag==0)
                {
                    $typeid=$typeid.','.$this->_sort_id_child_list($typeid);
                    $typeid=rtrim($typeid,',');
                    $whereSql .= ' AND arc.typeid IN ('.$typeid.')';
                }
                else
                {
                    $whereSql .= ' AND arc.typeid = ('.$typeid.')';
                }
            }



            $query = "SELECT sot.name as cname,arc.* FROM `crazy_archives` arc,`crazy_sort` sot $whereSql ORDER BY arc.uptime DESC";
            $this->load->model('admin/Archives_model');
            $list=$this->Archives_model->get_search($query);


            //从数据库中获取列表总数
            $num= $list->num_rows();

            $search_str="dopost=$dopost&typeid=$typeid&keyword=$keyword&flag=$flag";
            $params = array('total' => $num, 'listRows' => '10','pa'=>$search_str);

            $this->load->library('Page',$params);

            $query= $query."  limit {$this->page->limit} ";
            $reslut=$this->Archives_model->get_search($query);

            $data['list']=$reslut;

            $data['page']= $this->page->fpage();
            
             //获取自定义属性列表   
            $this->load->model('admin/Archives_model');
            $data['arcatt_all_list']= $this->Archives_model->get_arcatt_all();
            
            $this->load->view('admin/archives_member',$data);
        }
		
		
		
		
		
		
		
}